package com.sskj.gocoin.v.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.m.bean.TradeDeputeListBean;
import com.sskj.gocoin.p.fragment.TradeDeputeFragmentPst;
import com.sskj.gocoin.utils.BtnDoubleClickUtil;
import com.sskj.gocoin.utils.TipUtil;
import com.sskj.gocoin.v.activity.TradeActivity;
import com.sskj.gocoin.v.base.BaseViewPagerFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;


/**
 * 杠杆交易 -->委托订单
 * Created by Lee on 2018/2/28 0028.
 */

public class TradeDeputeFragment extends BaseViewPagerFragment<TradeActivity, TradeDeputeFragmentPst> {


    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.date_show)
    LinearLayout dateShow;
    //    @BindView(R2.id.ll_no_data)
    //    LinearLayout llNoData;
    Unbinder unbinder;
    @BindView(R2.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    //    private SlimAdapter slimAdapter;
    private BaseAdapter<TradeDeputeListBean> mAdapter;
    public String code = "";


    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public TradeDeputeFragmentPst getPresenter() {
        return new TradeDeputeFragmentPst();
    }

    @Override
    public void updateData() {
        if (!isHidden() && mPresenter != null) {
            mPresenter.loadData(code);
        }
    }

    @Override
    public void initView() {
        // 获取上一个Fragment过来的code值
        code = getArguments().getString("code");
        Log.i("msg001", "code:" + code);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dateShow.setVisibility(View.GONE);
        if (mAdapter == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity()).setFirstDraw(false));
            mAdapter = new BaseAdapter<TradeDeputeListBean>(R.layout.recy_item_depute, null, recyclerView) {
                @Override
                public void bind(ViewHolder holder, TradeDeputeListBean item) {
                    holder.setText(R.id.trade_cancel, App.INSTANCE.getString(R.string.strTradeDeputeFragment204));
                    holder.setText(R.id.tv_wtj, App.INSTANCE.getString(R.string.xmlrecy_item_depute50)+"  "+item.getBuyprice()); // 委托价
                    holder.setText(R.id.tv_bz, item.getPname());
                    holder.setText(R.id.tv_lx, "1".equals(item.getOtype()) ? App.INSTANCE.getString(R.string.strNewTradePositionFragment209) : App.INSTANCE.getString(R.string.strNewTradePositionFragment210));
                    holder.setText(R.id.tv_time, item.getAddtime());
                    holder.setTextColor(R.id.tv_lx, "1".equals(item.getOtype()) ? getResources().getColor(R.color.market_up) : getResources().getColor(R.color.market_down));
                    holder.setText(R.id.tv_wtl, App.INSTANCE.getString(R.string.xmlrecy_item_depute62)+"  "+item.getBuynum());  // 委托量
                    holder.setText(R.id.tv_zje, App.INSTANCE.getString(R.string.trade_sec)+"  "+item.getTotalprice());  // 保证金
                    holder.setText(R.id.tv_stopwin, App.INSTANCE.getString(R.string.xmllayout_head_view217)+"  "+item.getPoit_win());  //止盈价格
                    holder.setText(R.id.tv_stoploss, App.INSTANCE.getString(R.string.xmllayout_head_view236)+"  "+item.getPoit_loss());  // 止损价格
                    holder.setText(R.id.tv_sxf, App.INSTANCE.getString(R.string.xmlrecy_item_depute91)+"  "+item.getSxfee());  // 手续费
                    holder.getView(R.id.trade_cancel).setOnClickListener(v -> {
                        if (!BtnDoubleClickUtil.isFastDoubleClick()) {
                            TipUtil.getTip(getActivity(), App.INSTANCE.getString(R.string.strTradeDeputeFragment209), App.INSTANCE.getString(R.string.strTradeDeputeFragment210), () -> {
                                mPresenter.cancelTrade(item.getEn_id());
                            }).show();
                        }
                    });
                }
            };
        }
        mAdapter.setEmtyView();

        ClassicsHeader classicsHeader = new ClassicsHeader(getActivity());
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        refreshLayout.setRefreshHeader(classicsHeader);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh();
            mPresenter.loadData(code);
        });
        refreshLayout.autoRefresh();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.loadData(code);
        }
    }

    @Override
    public void updateData(String code) {
        this.code = code;
        if (!isHidden() && mPresenter != null) {
            mPresenter.loadData(code);
        }
    }

    public void updateData(List<TradeDeputeListBean> data) {
        mAdapter.setNewData(data);
        //        if (data == null || data.size() == 0) {
        ////            llNoData.setVisibility(View.VISIBLE);
        ////            refreshLayout.setVisibility(View.GONE);
        ////            if (data == null) {
        ////                ((TextView) llNoData.findViewById(R.id.tv_content)).setText(App.INSTANCE.getString(R.string.strTradeDeputeFragment211));
        ////            } else {
        ////                ((TextView) llNoData.findViewById(R.id.tv_content)).setText(App.INSTANCE.getString(R.string.strTradeDeputeFragment212));
        ////            }
        //
        //        } else {
        ////            llNoData.setVisibility(View.GONE);
        ////            refreshLayout.setVisibility(View.VISIBLE);
        //            mAdapter.addData(data);
        //        }
    }

}
